//
//  EntertainmentCollectionViewCell.m
//  WangI
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "EntertainmentCollectionViewCell.h"
#import "EntertainmentTableViewCell.h"
#import "EntertainmentTwoTableViewCell.h"
@implementation EntertainmentCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        [_tableView release];
    }
    return self;
}

- (void)setEntertainmentArray:(NSArray *)entertainmentArray {
    if (_entertainmentArray != entertainmentArray) {
        [_entertainmentArray release];
        _entertainmentArray = [entertainmentArray retain];
    }
    
    [self.tableView reloadData];
    
    
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = self.bounds;
    
    
    
}
- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
        [_tableView registerClass:[EntertainmentTableViewCell class] forCellReuseIdentifier:@"EntertainmentTableViewCellIdentifier"];
        [_tableView registerClass:[EntertainmentTwoTableViewCell class] forCellReuseIdentifier:@"EntertainmentTwoTableViewCellIdentifier"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.entertainmentArray.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EntertainmentModel *entertainmentModel = self.entertainmentArray[indexPath.row + 1];
    if (!entertainmentModel.imgextra.count) {
        EntertainmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EntertainmentTableViewCellIdentifier"];
        cell.entertainmentModel = entertainmentModel;
        return cell;
        
    } else {
        EntertainmentTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EntertainmentTwoTableViewCellIdentifier"];
        cell.entertainmentModel = entertainmentModel;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EntertainmentModel *entertainmentModel = self.entertainmentArray[indexPath.row + 1];

    [self.delegate showDetailPage:entertainmentModel.url withType:@"娱乐"];

}






- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    EntertainmentModel *entertainmentModel = self.entertainmentArray[indexPath.row + 1];
    if (entertainmentModel.imgextra.count) {
        return 120;
        
        
    } else {
        
        return 70;
    }
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.entertainmentArray.count) {
    self.entertainmentModel = self.entertainmentArray[0];
    NSString *titleStr = self.entertainmentModel.title;
    NSString *imaStr = self.entertainmentModel.imgsrc;
    self.titleArray = [[NSMutableArray alloc] init];
    self.imasrcArray = [[NSMutableArray alloc] init];
    [self.titleArray addObject:titleStr];
    [self.imasrcArray addObject:imaStr];
    for (NSDictionary *dic in [self.entertainmentModel valueForKey:@"ads"]) {
        [self.titleArray addObject:[dic valueForKey:@"title"]];
        [self.imasrcArray addObject:[dic valueForKey:@"imgsrc"]];
    }
    self.lunboScroll = [[ScrollWithImage alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200) withArray:self.imasrcArray withTitle:self.titleArray];
    
    return self.lunboScroll;
    } else {
        return nil;
    }
    
    
}

@end
